core: Add API to enumerate all refs
authorColin Walters <walters@verbum.org>
Mon, 27 Feb 2012 10:58:42 +0000 (05:58 -0500)
committerColin Walters <walters@verbum.org>
Mon, 27 Feb 2012 10:58:42 +0000 (05:58 -0500)
src/libostree/ostree-repo.c
src/libostree/ostree-repo.h
src/ostree/ot-builtin-prune.c
tests/t0000-basic.sh

index 17f3c7b48112dd710623c8ea862addfe812c122c..7f0eceff401178a7a0a1b84fce9b0689a8e1caf1 100644 (file)
@@ -1375,6 +1375,86 @@ create_empty_gvariant_dict (void)
   return g_variant_builder_end (&builder);
 }
 
+static gboolean
+enumerate_refs_recurse (OstreeRepo    *repo,
+                        GFile         *base,
+                        GFile         *dir,
+                        GHashTable    *refs,
+                        GCancellable  *cancellable,
+                        GError       **error)
+{
+  gboolean ret = FALSE;
+  GFileInfo *file_info = NULL;
+  GFileEnumerator *enumerator = NULL;
+  GFile *child = NULL;
+  GError *temp_error = NULL;
+
+  enumerator = g_file_enumerate_children (dir, OSTREE_GIO_FAST_QUERYINFO,
+                                          G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
+                                          cancellable, error);
+  if (!enumerator)
+    goto out;
+
+  while ((file_info = g_file_enumerator_next_file (enumerator, cancellable, &temp_error)) != NULL)
+    {
+      g_clear_object (&child);
+      child = g_file_get_child (dir, g_file_info_get_name (file_info));
+      if (g_file_info_get_file_type (file_info) == G_FILE_TYPE_DIRECTORY)
+        {
+          if (!enumerate_refs_recurse (repo, base, child, refs, cancellable, error))
+            goto out;
+        }
+      else if (g_file_info_get_file_type (file_info) == G_FILE_TYPE_REGULAR)
+        {
+          char *contents;
+          gsize len;
+
+          if (!g_file_load_contents (child, cancellable, &contents, &len, NULL, error))
+            goto out;
+
+          g_strchomp (contents);
+
+          g_hash_table_insert (refs, g_file_get_relative_path (base, child), contents);
+        }
+
+      g_clear_object (&file_info);
+    }
+  if (temp_error != NULL)
+    {
+      g_propagate_error (error, temp_error);
+      goto out;
+    }
+
+  ret = TRUE;
+ out:
+  g_clear_object (&file_info);
+  g_clear_object (&child);
+  return ret;
+}
+
+gboolean
+ostree_repo_list_all_refs (OstreeRepo       *repo,
+                           GHashTable      **out_all_refs,
+                           GCancellable     *cancellable,
+                           GError          **error)
+{
+  gboolean ret = FALSE;
+  GHashTable *ret_all_refs = NULL;
+  GFile *heads_dir = NULL;
+
+  ret_all_refs = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
+
+  heads_dir = g_file_resolve_relative_path (ostree_repo_get_path (repo), "refs/heads");
+  if (!enumerate_refs_recurse (repo, heads_dir, heads_dir, ret_all_refs, cancellable, error))
+    goto out;
+
+  ret = TRUE;
+  ot_transfer_out_value (out_all_refs, &ret_all_refs);
+ out:
+  g_clear_object (&heads_dir);
+  return ret;
+}
+
 gboolean      
 ostree_repo_write_ref (OstreeRepo  *self,
                        const char  *remote,
index 19494464281c186576d72d92d7ba8e4101d21af8..ca1fecf1f1ff11f85aa8f0ef990f16409351382c 100644 (file)
@@ -130,6 +130,11 @@ gboolean      ostree_repo_write_ref (OstreeRepo  *self,
                                      const char  *rev,
                                      GError     **error);
 
+gboolean      ostree_repo_list_all_refs (OstreeRepo       *repo,
+                                         GHashTable      **out_all_refs,
+                                         GCancellable     *cancellable,
+                                         GError          **error);
+
 gboolean      ostree_repo_load_variant (OstreeRepo  *self,
                                         OstreeObjectType expected_type,
                                         const char    *sha256, 
index 5bfc74a479a0a0552353709db8d54d105e27a230..d747d690fee289485c17431639fe5c35ae9f3067 100644 (file)
@@ -225,84 +225,6 @@ object_iter_callback (OstreeRepo    *repo,
   g_free (key);
 }
 
-static gboolean
-add_refs_recurse (OstreeRepo    *repo,
-                  GFile         *base,
-                  GFile         *dir,
-                  GHashTable    *refs,
-                  GCancellable  *cancellable,
-                  GError       **error)
-{
-  gboolean ret = FALSE;
-  GFileInfo *file_info = NULL;
-  GFileEnumerator *enumerator = NULL;
-  GFile *child = NULL;
-  GError *temp_error = NULL;
-
-  enumerator = g_file_enumerate_children (dir, OSTREE_GIO_FAST_QUERYINFO,
-                                          G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
-                                          cancellable, error);
-  if (!enumerator)
-    goto out;
-
-  while ((file_info = g_file_enumerator_next_file (enumerator, cancellable, &temp_error)) != NULL)
-    {
-      g_clear_object (&child);
-      child = g_file_get_child (dir, g_file_info_get_name (file_info));
-      if (g_file_info_get_file_type (file_info) == G_FILE_TYPE_DIRECTORY)
-        {
-          if (!add_refs_recurse (repo, base, child, refs, cancellable, error))
-            goto out;
-        }
-      else if (g_file_info_get_file_type (file_info) == G_FILE_TYPE_REGULAR)
-        {
-          char *contents;
-          gsize len;
-
-          if (!g_file_load_contents (child, cancellable, &contents, &len, NULL, error))
-            goto out;
-
-          g_strchomp (contents);
-
-          g_hash_table_insert (refs, g_file_get_relative_path (base, child), contents);
-        }
-
-      g_clear_object (&file_info);
-    }
-  if (temp_error != NULL)
-    {
-      g_propagate_error (error, temp_error);
-      goto out;
-    }
-
-  ret = TRUE;
- out:
-  g_clear_object (&file_info);
-  g_clear_object (&child);
-  return ret;
-}
-
-static gboolean
-list_all_refs (OstreeRepo       *repo,
-               GHashTable      **out_all_refs,
-               GCancellable     *cancellable,
-               GError          **error)
-{
-  gboolean ret = FALSE;
-  GHashTable *ret_all_refs = NULL;
-  GFile *heads_dir = NULL;
-
-  ret_all_refs = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
-
-  heads_dir = g_file_resolve_relative_path (ostree_repo_get_path (repo), "refs/heads");
-  if (!add_refs_recurse (repo, heads_dir, heads_dir, ret_all_refs, cancellable, error))
-    goto out;
-
-  ret = TRUE;
-  ot_transfer_out_value (out_all_refs, &ret_all_refs);
- out:
-  return ret;
-}
 
 gboolean
 ostree_builtin_prune (int argc, char **argv, GFile *repo_path, GError **error)
@@ -333,7 +255,7 @@ ostree_builtin_prune (int argc, char **argv, GFile *repo_path, GError **error)
   data.n_reachable = 0;
   data.n_unreachable = 0;
 
-  if (!list_all_refs (repo, &all_refs, cancellable, error))
+  if (!ostree_repo_list_all_refs (repo, &all_refs, cancellable, error))
     goto out;
 
   g_hash_table_iter_init (&hash_iter, all_refs);
index 2a00b91a732d2ecc51cdd788c245575888df8d42..c3cd7a51d4527d9b60de2920894159b405f187c9 100755 (executable)
@@ -19,7 +19,7 @@
 
 set -e
 
-echo "1..24"
+echo "1..25"
 
 . libtest.sh
 
@@ -181,3 +181,7 @@ EOF
 cd ${test_tmpdir}/checkout-test2-4
 $OSTREE commit -b test2 -s "with statoverride" --statoverride=../test-statoverride.txt
 echo "ok commit statoverridde"
+
+cd ${test_tmpdir}
+$OSTREE prune
+echo "ok prune didn't fail"